﻿using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Web.Configuration;
using WebApplicationRating.DB.model;


namespace WebApplicationRating
{
    public partial class MyImage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void ImageGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int IMGID = Convert.ToInt32(GridView2.DataKeys[e.RowIndex].Value);

            string IMGIDstr = Convert.ToString(GridView2.DataKeys[e.RowIndex].Value);
            string CATIDstr = GridView2.Rows[e.RowIndex].Cells[4].Text;

            //for test echoes
            //string myString = " imageid " + Convert.ToString(GridView2.DataKeys[e.RowIndex].Value);
            //lblException.Text = myString + "categoryid " + CATIDstr;

            try
            {
                using (imageEntities ctx = new imageEntities())
                {

                    var count = ctx.ImageCategories.Count(t => t.imageid == IMGID);
                    lblException.Text = "count = " + count;

                    if (count > 1)
                    {
                        try
                        {
                            var newIntImg = Convert.ToInt32(IMGIDstr);
                            var newIntCat = Convert.ToInt32(CATIDstr);

                            //Get the specific image category from Database

                            ImageCategory u = (from u1 in ctx.ImageCategories

                                               where (u1.imageid == newIntImg && u1.categoryid == newIntCat)
                                               select u1).First();


                            //Delete it from memory

                            ctx.DeleteObject(u);

                            //Save to database

                            ctx.SaveChanges();

                            lblException.Text = "Category Deleted for Image!";
                        }

                        catch
                        {
                            lblException.Text = "Delete Unable to Perform. Contact Support.";
                        }


                    }
                    if (count == 1)
                    {
                        try
                        {
                            var newIntImg = Convert.ToInt32(IMGIDstr);

                            //Get the specific image category from Database

                            WebApplicationRating.DB.model.Image u = (from u1 in ctx.Images

                                                                     where (u1.imageid == newIntImg)
                                                                     select u1).First();
                            //Delete it from memory

                            ctx.DeleteObject(u);

                            //Save to database

                            ctx.SaveChanges();

                            lblException.Text = "Image Deleted!";
                        }
                        catch
                        {
                            lblException.Text = "Delete Unable to Perform. Contact Support.";
                        }
                    }

                }

                //GridViewRow row = (GridViewRow)GridView2.Rows[e.RowIndex];
                //Label lbldeleteID = (Label)row.FindControl("name");
            }
            catch { }

        }
      
 protected void ImageGridView_RowDeleted(object sender, GridViewDeletedEventArgs e)
 {

            //lblException.Text = "Made It";
            if (e.Exception != null)
            {
                if (e.Exception.InnerException.Message.Substring(0, 61) == "The DELETE statement conflicted with the REFERENCE constraint")
                {
                    lblException.Text = "Images are Assigned, Cannot Delete. Contact Support for More Information.";
                }
                else
                {
                    lblException.Text = "Contact Support. " + e.Exception.InnerException.Message.Substring(0, 61);
                }

                e.ExceptionHandled = true;
            }
            else if (e.AffectedRows != 0)
            {
                //Display Success Message that Record Deleted Successfully 
                lblException.Text = "Delete Successful.";
            }

            else
            {
                //this happens in onRowDeleting instead of here which is rowDeleted
                //see if just the image category record should be deleted as image has more than one category

            }

 } 



        protected void SqlDataSource_Selecting(object sender,SqlDataSourceSelectingEventArgs e)
        {
            int curUserId = SharedItems.Field2; //Convert.ToInt32(WebConfigurationManager.AppSettings["UserID"].ToString());
            e.Command.Parameters["@curUSR"].Value = curUserId;
        } 


    }
}